package com.yjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.mapper.PropertyInfoMapper;
import com.yjx.query.PropertyInfoQuery;
import com.yjx.service.PropertyInfoService;
import com.yjx.util.AssertUtil;
import com.yjx.vo.PropertyInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 10657
* @description 针对表【property_info】的数据库操作Service实现
* @createDate 2023-05-11 15:55:22
*/
@Service
public class PropertyInfoServiceImpl extends ServiceImpl<PropertyInfoMapper, PropertyInfo> implements PropertyInfoService{
    @Resource
    PropertyInfoMapper propertyInfoMapper;

    @Override
    public PageInfo<PropertyInfo> queryPropertyAll(PropertyInfoQuery propertyInfoQuery) {

        // 开启分页
        PageHelper.startPage(propertyInfoQuery.getPage(), propertyInfoQuery.getLimit());
        List<PropertyInfo> lists = propertyInfoMapper.queryPropertyAll(propertyInfoQuery);
        PageInfo<PropertyInfo> propertyInfos = new PageInfo(lists);
        return propertyInfos;
    }

    @Override
    public void deletePropertyInfo(List<Integer> ids) {
        //判断ids有没有
        AssertUtil.isTrue(ids == null || ids.size() == 0,"没有要删除的数据");
        //判断ids合不合理
        Integer count = propertyInfoMapper.selectCount(new QueryWrapper<PropertyInfo>().in("id",ids));
        AssertUtil.isTrue(ids.size() != count,"存在不合理的数据删除，妄想攻击我!!!");
        Integer row = propertyInfoMapper.deletePropertyInfo(ids);
        AssertUtil.isTrue(row<ids.size(),"删除失败");
    }

    @Override
    public Integer add(PropertyInfo propertyInfo) {
        AssertUtil.isTrue(propertyInfo.getStartDate()==null,"输入不合理");
        AssertUtil.isTrue(propertyInfo.getEndDate()==null,"输入不合理");
        int row = propertyInfoMapper.insert(propertyInfo);
        AssertUtil.isTrue(row<1,"添加失败");
        return row;
    }

    @Override
    public int updateData(Integer id) {
        PropertyInfo propertyInfo = propertyInfoMapper.selectById(id);
        AssertUtil.isTrue(propertyInfo == null,"错误查询");
        AssertUtil.isTrue(propertyInfo.getStatus()==1,"已经缴费，不需要再缴费了");
        propertyInfo.setStatus(1);
        int row = propertyInfoMapper.updateById(propertyInfo);
        AssertUtil.isTrue(row<1,"更新失败");
        return row;
    }
}







